<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 5/25/21
 * Time: 8:55 PM
 */

namespace app\api\controller\v1;

use app\api\model\Product as ProductModel;
use app\api\model\Store as StoreModel;
use app\api\model\StoreGroup as StoreGroupModel;
use app\api\model\StoreUser as StoreUserModel;
use app\api\model\UserMoney;
use app\lib\exception\TokenException;
use LinCmsTp5\exception\ParameterException;
use think\facade\Cache;
use app\api\model\User;

// 店面信息
class Store extends Base
{
    /**
     * 获取店面列表
     */
    public function getDataAll()
    {
        return StoreModel::order('order_sort desc')->select()->toArray();
    }


    /**
     * 获取店面列表
     * @validate('PagingParameter')
     */
    public function getDataList($name,$page,$limit)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        $query = StoreModel::where($where)->order('order_sort desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            $item['store_url'] = config('setting.qr_prefix').'store_'.$item['id'].'.png';
            $user = User::where('store_id',$item['id'])->where('type',1)->select()->toArray();
            $userIds = array_column($user,'id');
            $item['invite_money'] = UserMoney::where('user_id','in',$userIds)
                ->where('status','eq',2)->sum('money');
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取店面详情
     * @validate('IdMustBePositiveInt')
     */
    public function getDataInfo($id)
    {
        return StoreModel::get($id);
    }

    /**
     * 添加店面信息
     * @validate('CheckStore')
     */
    public function createData($name,$coordinate,$start_day_num,$end_day_num,$address,$phone,$order_sort,$desc,$remarks)
    {
        $mode = StoreModel::create([
            'name' => $name,
            'coordinate' => $coordinate,
            'start_day_num' => $start_day_num,
            'end_day_num' => $end_day_num,
            'address' => $address,
            'phone' => $phone,
            'order_sort' => $order_sort,
            'desc' => $desc,
            'remarks' => $remarks
        ]);
        $data['scene'] = 'store-' . $mode->id;
        $data['page'] = 'pages/invite/store';
        $data['width'] = 280;
        $name = 'store_' . $mode->id;
        $this->setCodePub($data,$name);
        return writeJson(201, [], '添加成功！');
    }

    /**
     * 修改店面信息
     * @validate('CheckStore')
     */
    public function updateData($id,$name,$coordinate,$start_day_num,$end_day_num,$address,$phone,$order_sort,$desc,$remarks)
    {
        $info = StoreModel::get($id);
        if($info) {
            $info->name = $name;
            $info->coordinate = $coordinate;
            $info->start_day_num = $start_day_num;
            $info->end_day_num = $end_day_num;
            $info->address = $address;
            $info->phone = $phone;
            $info->order_sort = $order_sort;
            $info->desc = $desc;
            $info->remarks = $remarks;
            $info->save();
//            $data['scene'] = 'store-' .$id;
//            $data['page'] = 'pages/invite/store';
//            $data['width'] = 280;
//            $name = 'store_' . $id;
//            $this->setCodePub($data,$name);
        }
        return writeJson(202, [], '修改成功！');
    }

    /**
     * 删除店面信息
     * @validate('IdMustBePositiveInt')
     */
    public function deleteData($id)
    {
        // 关联门店
        $product = ProductModel::where('store_id',$id)->select();
        if(!$product->isEmpty()) {
            throw new ParameterException([
                'msg' => [
                    'store' => '店面下还有关联产品,不能删除店面！'
                ]
            ]);
        }
        // 关联分组
        $shopOwner = StoreGroupModel::where('store_id',$id)->select();
        if(!$shopOwner->isEmpty()) {
            throw new ParameterException([
                'msg' => [
                    'store' => '店面下还有关联分组,不能删除店面！'
                ]
            ]);
        }
        // 关联员工
        $shopStaff = StoreUserModel::where('store_id',$id)->select();
        if(!$shopStaff->isEmpty()) {
            throw new ParameterException([
                'msg' => [
                    'store' => '店面下还有关联员工,不能删除店面！'
                ]
            ]);
        }
        StoreModel::destroy($id);
        return writeJson(202, [], '删除成功！');
    }
}